function [saccades]=get_saccades(pupil_pos,pupil_diam,nbr_iframes,idata,imeta_data)

throw_away=[];
for vnd=1:length(nbr_iframes)-1
    throw_away(vnd)=sum(nbr_iframes(1:vnd));
end

delta=[];
for ind=1:size(pupil_pos,2)-1
    dx=abs(pupil_pos(1,ind)-pupil_pos(1,ind+1));
    dy=abs(pupil_pos(2,ind)-pupil_pos(2,ind+1));
    delta(ind)=sqrt(dx^2+dy^2);
end

delta_thresh=round(median(delta)+(std(delta)*3));
cnt=1;
for znd=4:length(delta)-4
    cur=delta(znd-3:znd+4)>delta_thresh;
    if sum(cur(4:5))==2 & cur(1:3)==0 & cur(6:end)==0
        ini_saccades(cnt)=znd;
        cnt=cnt+1;
    end
end

a=[];
b=[];
mat=[];
for ynd=1:length(ini_saccades)
    a(ynd)=min(pupil_diam(ini_saccades(ynd)-3:ini_saccades(ynd)+4));
    b(ynd)=max(pupil_diam(ini_saccades(ynd)-3:ini_saccades(ynd)+4));
    mat(ynd)=abs(a(ynd)-b(ynd));
end

ini_saccades(mat>40)=[];

for bnd=1:length(throw_away)
    ini_saccades(ismember(ini_saccades,throw_away(bnd)-10:throw_away(bnd)+10))=[];
end


ooo=[];
cnt=1;
cnt2=1;
uhoh=[];
for knd=1:length(ini_saccades)
    if abs(mean(pupil_diam(ini_saccades(knd)-5:ini_saccades(knd)-2))-mean(pupil_diam(ini_saccades(knd):ini_saccades(knd)+2)))>1 ...
            & (sum(delta(ini_saccades(knd)-8:ini_saccades(knd)+8)>delta_thresh==-1)+sum(delta(ini_saccades(knd)-8:ini_saccades(knd)+8)>delta_thresh==1))/2==1
        ooo(cnt)=ini_saccades(knd);
        cnt=cnt+1;
    else
        uhoh(cnt2)=ini_saccades(knd);
        cnt2=cnt2+1;
    end
end

rescued=[];
cnt=1;
cnt2=1;
xxx=[];
for knd=1:length(uhoh)
    sac(knd,:)=delta(uhoh(knd)-8:uhoh(knd)+8);
    if (sum(sac(knd,:)>delta_thresh==-1)+sum(sac(knd,:)>delta_thresh==1))/2==1 & (max(sac(knd,:))-min(sac(knd,:)))>=20
        rescued(cnt)=uhoh(knd);
        cnt=cnt+1;
    else
        xxx(cnt2)=uhoh(knd);
        cnt2=cnt2+1;
    end
end

saccades=sort([ooo rescued]);
